/**
 *ButtonFactory.java
 *@anthor Daniel Liu
 *2006-4-28 3:30:18
 **/
package org.lex.swing;

import javax.swing.AbstractButton;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

public class ButtonFactory {
	/**
	 * JMenuItem.set
	 * 
	 * @param text
	 *            like "&File"
	 * @return menu item
	 */
	public static JMenuItem newMenuItem(String text) {
		return setupButton(new JMenuItem(), text);
	}

	public static JMenu newMenu(String text) {
		return setupButton(new JMenu(), text);
	}

	/**
	 * setActionCommand() <br>
	 * setMnemonic()<br>
	 * setText()
	 */
	public static <T extends AbstractButton> T setupButton(T button, String text) {
		button.setActionCommand(text);
		int index = text.indexOf('&');
		char ch = text.charAt(index + 1);
		button.setMnemonic(ch);
		StringBuilder buf = new StringBuilder();
		buf.append(text.substring(0, index));
		buf.append(text.substring(index + 1));
		button.setText(buf.toString());
		return button;
	}
}
